package com.doublefish.doublefishrobot.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;

/**
 * 作者:dugaofeng on 2024/10/18 15:07
 */
public class CommonDialog extends Dialog implements View.OnClickListener {

    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    private View line;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;

    private int positiveBtnColor = 0;
    private int negativeBtnColor = 0;
    private boolean showPositiveBtn = true;
    private boolean showNegativeBtn = true;

    private boolean autoDismissWhenClick = true;

    public CommonDialog(Context context, String title, String content, OnCloseListener listener) {
        super(context,  R.style.dialog);

        this.mContext = context;
        this.content = content;
        this.title = title;
        this.listener = listener;
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommonDialog setContent(String content) {
        this.content = content;
        return this;
    }

    public CommonDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public CommonDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    public CommonDialog setPositiveButtonColor(int color) {
        this.positiveBtnColor = color;
        return this;
    }

    public CommonDialog setNegativeButtonColor(int color) {
        this.negativeBtnColor = color;
        return this;
    }

    public CommonDialog setShowPositiveButton(boolean show) {
        this.showPositiveBtn = show;
        return this;
    }

    public CommonDialog setShowNegativeButton(boolean show) {
        this.showNegativeBtn = show;
        return this;
    }

    public CommonDialog setAutoDismissWhenClick(boolean autoDismissWhenClick) {
        this.autoDismissWhenClick = autoDismissWhenClick;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_common);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        contentTxt = (TextView) findViewById(R.id.content);
        titleTxt = (TextView) findViewById(R.id.title);
        submitTxt = (TextView) findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView) findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);
        line = findViewById(R.id.line);

        refreshUI();
    }

    public void refreshUI() {
        if (!TextUtils.isEmpty(title)) {
            titleTxt.setText(title);
        }

        contentTxt.setText(content);

        if (!TextUtils.isEmpty(positiveName)) {
            submitTxt.setText(positiveName);
        }

        if (!TextUtils.isEmpty(negativeName)) {
            cancelTxt.setText(negativeName);
        }

        if (positiveBtnColor != 0) {
            submitTxt.setTextColor(positiveBtnColor);
        }
        if (negativeBtnColor != 0) {
            cancelTxt.setTextColor(negativeBtnColor);
        }

        if (!showPositiveBtn) {
            line.setVisibility(View.GONE);
            submitTxt.setVisibility(View.GONE);
        }
        if (!showNegativeBtn) {
            line.setVisibility(View.GONE);
            cancelTxt.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                if (listener != null) {
                    listener.onClick(this, false);
                }

                break;
            case R.id.submit:
                if (listener != null) {
                    listener.onClick(this, true);
                }
                break;
        }
        if (autoDismissWhenClick) {
            dismiss();
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm);
    }
}
